<?php
/**
 * DIY页面后台管理
 * @author Stream
 *
 */
// 加载后台控制器
tsload(APPS_PATH.'/admin/Lib/Action/AdministratorAction.class.php');
class AdminAction extends AdministratorAction {
	
	function _initialize(){
		$this->pageTitle['index'] = '用户账户信息';
        $this->pageTitle['search_business'] = '用户交易情况';
        $this->pageTitle['editUserInfo'] = '修改资金';
        $this->pageTitle['com_time'] = '设置比赛时间';
        $this->pageTitle['com_info'] = '比赛信息';
        $this->pageTitle['com_search_business'] = '比赛交易信息';

        $this->pageTitle['com_cp'] = '比赛平仓信息';
        $this->pageTitle['cp'] = '平仓信息';

 $this->pageTitle['check_ip'] = '比赛相同IP检测';
		parent::_initialize();
	}
	/**
	 * 初始化用户列表管理菜单
	 * @param string $type 列表类型，index、pending、dellist
	 */
	private function _initPageListAdminMenu($type) {
		// tab选项

		$this->pageTab[] = array('title'=>'用户账户信息','tabHash'=>'index','url'=>U('bitcoin/Admin/index'));
		$this->pageTab[] = array('title'=>'用户交易情况','tabHash'=>'search_business','url'=>U('bitcoin/Admin/search_business'));
        $this->pageTab[] = array('title'=>'平仓信息','tabHash'=>'cp','url'=>U('bitcoin/Admin/cp'));
        $this->pageTab[] = array('title'=>'设置比赛时间','tabHash'=>'com_time','url'=>U('bitcoin/Admin/com_time'));
        $this->pageTab[] = array('title'=>'比赛信息','tabHash'=>'com_info','url'=>U('bitcoin/Admin/com_info'));
        $this->pageTab[] = array('title'=>'比赛交易信息','tabHash'=>'com_search_business','url'=>U('bitcoin/Admin/com_search_business'));
        $this->pageTab[] = array('title'=>'比赛平仓信息','tabHash'=>'com_cp','url'=>U('bitcoin/Admin/com_cp'));


        $this->pageTab[] = array('title'=>'比赛相同IP检测','tabHash'=>'check_ip','url'=>U('bitcoin/Admin/check_ip'));
	}



public function com_info(){
    $_REQUEST['tabHash'] = 'com_info';
    $this->_initPageListAdminMenu('com_info');
    $this->_listpk = 'uid';
    $this->pageButton[] = array('title'=>'搜索用户账户资金','onclick'=>"admin.fold('search_form')");
    $this->pageButton[]=array('title'=>'清空所有比赛数据','onclick'=> 'location.href=\''.U('bitcoin/Admin/delete_com_data',array('f'=>'com')).'\'');
    $this->pageButton[] = array('title'=>'批量禁止/取消禁止用户','onclick'=>"admin.ban_com()");

    $this->searchKey = array('uname');
    $this->pageKeyList = array('id','uid','uname','dollar','f_dollar','isban','login_ip','action');
    $data3['uname'] = array('like','%'.t($_POST['uname']).'%');
    // 数据的格式化与listKey保持一致
    $listData['data'] = D('bitcoin_com_info')->where($data3)->order('login_ip asc')->select();
    foreach($listData['data'] as $key=>$v)
    {
        if( $listData['data'][$key]['isban']==0){
            $listData['data'][$key]['isban']='允许使用应用';
            $listData['data'][$key]['action']='<a href="'.U('bitcoin/Admin/editUserInfo',array('uid'=>$v['uid'],'f'=>'com')).'">编辑</a>  | <a href="'
                .U('bitcoin/Admin/doDelUserInfo',array('uid'=>$v['uid'],'f'=>'com')).'">删除</a>  | <a href="'
                .U('bitcoin/Admin/banUser1',array('uid'=>$v['uid'],'f'=>'com')).'">禁止用户</a>';
        }
        else{
            $listData['data'][$key]['isban']='禁止使用应用';
            $listData['data'][$key]['action']='<a href="'.U('bitcoin/Admin/editUserInfo',array('uid'=>$v['uid'],'f'=>'com')).'">编辑</a>  | <a href="'
                .U('bitcoin/Admin/doDelUserInfo',array('uid'=>$v['uid'],'f'=>'com')).'">删除</a>  | <a href="'
                .U('bitcoin/Admin/banUser1',array('uid'=>$v['uid'],'f'=>'com')).'">取消禁止用户</a>';
        }

    }
    // 列表批量操作按钮
    //   $this->savePostUrl = U('page/Admin/search');
    $this->displayList($listData);

}
    public function doDelUserInfo(){
        $data['uid']=$_GET['uid'];
        if($_GET['f']=='index'){
        $rs=D('bitcoin_info')->where($data)->delete();
            D('bitcoin_entrust')->where($data)->delete();
            D('bitcoin_closeposition')->where($data)->delete();
        }
        if($_GET['f']=='com'){
            $rs=D('bitcoin_com_info')->where($data)->delete();
            D('bitcoin_com_entrust')->where($data)->delete();
            D('bitcoin_com_closeposition')->where($data)->delete();


        }
        if($rs)
        {
            $this->success('删除成功。');
        }
        else{
            $this->error('删除失败。');
        }

    }
    public function editUserInfo(){
        $this->_initPageListAdminMenu('editUserInfo');
        if($_GET['uid'])
        {
            $data['uid']=$_GET['uid'];
            if($_GET['f']=='index'){
            $user_info=  D('bitcoin_info')->where($data)->select();
        }
            if($_GET['f']=='com'){
                $user_info=  D('bitcoin_com_info')->where($data)->select();
            }
        }

        $this->pageKeyList=array('uid','dollar','f_dollar');
        $this->savePostUrl=U('bitcoin/Admin/doeditUserInfo',array('f'=>$_GET['f']));
        $this->displayConfig($user_info['0']);

    }
    public function doeditUserInfo(){
        if($_GET['f']=='index'){
        $data=D('bitcoin_info')->create();
            $condition['uid']=$data['uid'];
            $rs=D('bitcoin_info')->where($condition)->save($data);

        }
        if($_GET['f']=='com'){
            $data=D('bitcoin_com_info')->create();
            $condition['uid']=$data['uid'];
            $rs=D('bitcoin_com_info')->where($condition)->save($data);
        }

          if($rs)
            {
                $this->success('保存成功。');
            }
            else{
                $this->error('保存失败。');

            }

    }
	/**
	 * 页面列表
	 */
	function index(){
		
		$_REQUEST['tabHash'] = 'index';
		// 初始化diy页面列表管理菜单
		$this->_initPageListAdminMenu('index');
        $this->pageButton[] = array('title'=>'搜索用户账户资金','onclick'=>"admin.fold('search_form')");
        $this->pageButton[]=array('title'=>'清空模拟数据','onclick'=> 'location.href=\''.U('bitcoin/Admin/delete_com_data',array('f'=>'index')).'\'');
        $this->pageButton[] = array('title'=>'批量禁止/取消禁止用户','onclick'=>"admin.ban_index()");

        $this->searchKey = array('uname');
        $this->pageKeyList = array('id','uid','uname','dollar','f_dollar','isban','login_ip','action');
        $data3['uname'] = array('like','%'.t($_POST['uname']).'%');
        		// 数据的格式化与listKey保持一致
        $listData['data'] = D('bitcoin_info')->where($data3)->order('login_ip asc')->select();
        foreach($listData['data'] as $key=>$v)
        {
            if( $listData['data'][$key]['isban']==0){
                $listData['data'][$key]['isban']='允许使用应用';
                $listData['data'][$key]['action']='<a href="'.U('bitcoin/Admin/editUserInfo',array('uid'=>$v['uid'],'f'=>'index')).'">编辑</a>  | <a href="'
                    .U('bitcoin/Admin/doDelUserInfo',array('uid'=>$v['uid'],'f'=>'index')).'">删除</a>  | <a href="'
                    .U('bitcoin/Admin/banUser1',array('uid'=>$v['uid'],'f'=>'index')).'">禁止用户</a>';
            }
            else{
                $listData['data'][$key]['isban']='禁止使用应用';
                $listData['data'][$key]['action']='<a href="'.U('bitcoin/Admin/editUserInfo',array('uid'=>$v['uid'],'f'=>'index')).'">编辑</a>  | <a href="'
                    .U('bitcoin/Admin/doDelUserInfo',array('uid'=>$v['uid'],'f'=>'index')).'">删除</a>  | <a href="'
                    .U('bitcoin/Admin/banUser1',array('uid'=>$v['uid'],'f'=>'index')).'">取消禁止用户</a>';
            }

        }
		// 列表批量操作按钮
	 //   $this->savePostUrl = U('page/Admin/search');
		$this->displayList($listData);

	}

   public function banUser(){
         $condition['uid']=$_GET['uid'];
         if($_GET['f']=='index'){
             $info=D('bitcoin_info')->where($condition)->select();
         }
         if($_GET['f']=='com'){
             $info=D('bitcoin_com_info')->where($condition)->select();
         }

         if($info['0']['isban']==0){
             $data['isban']=1;
         }
         else{
             $data['isban']=0;
         }
         if($_GET['f']=='index'){
         $rs=D('bitcoin_info')->where($condition)->save($data);
         }
         if($_GET['f']=='com'){
             $rs=D('bitcoin_com_info')->where($condition)->save($data);
         }
       if(!$rs) {
           $return['status'] = 0;
           $return['data'] = L('PUBLIC_ADMIN_OPRETING_ERROR');				// 操作失败
       } else {
           //关联删除用户其他信息，执行删除用户插件.
           $return['status'] = 1;
           $return['data'] = L('PUBLIC_ADMIN_OPRETING_SUCCESS');			// 操作成功
       }
       exit(json_encode($return));



     }

    function com_time(){

        $_REQUEST['tabHash'] = 'com_time';
        // 初始化diy页面列表管理菜单
        $this->_initPageListAdminMenu('com_time');
        $this->pageKeyList = array('start_time', 'end_time','introduce');
        $this->displayConfig();

    }

    function search_business(){
        $_REQUEST['tabHash'] = 'search_business';
        // 初始化diy页面列表管理菜单
        $this->_initPageListAdminMenu('search_business');
        $this->pageButton[] = array('title'=>'搜索用户交易情况','onclick'=>"admin.fold('search_form')");
        $this->pageButton[]=array('title'=>'清空模拟数据','onclick'=> 'location.href=\''.U('bitcoin/Admin/delete_com_data',array('f'=>'index')).'\'');
        $this->searchKey = array('uname');
        $this->pageKeyList = array('id','uid','uname','type','price','number','state','entrust_time','turnover_time');

        if($_POST['uname'] == null){
            $entrust = D('bitcoin_entrust')->order('id desc')->select();
            foreach($entrust as $k=>$v){
                $condition['uid']=$entrust[$k]['uid'];
                $info=D('bitcoin_info')->where($condition)->select();
                $entrust[$k]['uname']= $info['0']['uname'];


            }
        }

        else{
            $data3['uname'] = array('like','%'.t($_POST['uname']).'%');
            $user_info=  D('bitcoin_info')->where($data3)->select();
            //$data['uname'] = array('like','%'.t($_POST['uname']).'%');
            $data['uid']=$user_info['0']['uid'];
            // 数据的格式化与listKey保持一致
            $entrust = D('bitcoin_entrust')->where($data)->order('id desc')->select();
            foreach($entrust as $k=>$v){
                $entrust[$k]['uname']=$user_info['0']['uname'];
            }
        }

        foreach($entrust as $k=>$v){

            $entrust[ $k]['entrust_time']= date('Y-m-d H:i',$entrust[ $k]['entrust_time'] );
            $entrust[ $k]['turnover_time']= date('Y-m-d H:i',$entrust[ $k]['turnover_time'] );

        if($entrust[$k]['type']==0){
            $entrust[$k]['type']="买入";

        }
        if($entrust[$k]['type']==1){
            $entrust[$k]['type']="卖出";

        }
            if($entrust[$k]['state']==0){
                $entrust[$k]['state']="未成交";

            }
            if($entrust[$k]['state']==1){
                $entrust[$k]['state']="已成交";

            }
    }
        $listData['data']=$entrust;

        // 列表批量操作按钮
        //   $this->savePostUrl = U('page/Admin/search');
        $this->displayList($listData);

    }

    function com_search_business(){
        $_REQUEST['tabHash'] = 'com_search_business';
        // 初始化diy页面列表管理菜单
        $this->_initPageListAdminMenu('com_search_business');
        $this->pageButton[] = array('title'=>'搜索用户交易情况','onclick'=>"admin.fold('search_form')");
        $this->pageButton[]=array('title'=>'清空所有比赛数据','onclick'=> 'location.href=\''.U('bitcoin/Admin/delete_com_data',array('f'=>'com')).'\'');
        $this->searchKey = array('uname');
        $this->pageKeyList = array('id','uid','uname','type','price','number','state','entrust_time','turnover_time');

        if($_POST['uname'] == null){
            $entrust = D('bitcoin_com_entrust')->order('id desc')->select();
            foreach($entrust as $k=>$v){
                $condition['uid']=$entrust[$k]['uid'];
                $info=D('bitcoin_com_info')->where($condition)->select();
                $entrust[$k]['uname']= $info['0']['uname'];


            }
        }

        else{
        $data3['uname'] = array('like','%'.t($_POST['uname']).'%');
        $user_info=  D('bitcoin_com_info')->where($data3)->select();
        //$data['uname'] = array('like','%'.t($_POST['uname']).'%');
        $data['uid']=$user_info['0']['uid'];
        // 数据的格式化与listKey保持一致
        $entrust = D('bitcoin_com_entrust')->where($data)->order('id desc')->select();
            foreach($entrust as $k=>$v){
                $entrust[$k]['uname']=$user_info['0']['uname'];
            }
        }


        foreach($entrust as $k=>$v){
            $entrust[ $k]['entrust_time']= date('Y-m-d H:i',$entrust[ $k]['entrust_time'] );
            $entrust[ $k]['turnover_time']= date('Y-m-d H:i',$entrust[ $k]['turnover_time'] );

            if($entrust[$k]['type']==0){
                $entrust[$k]['type']="买入";

            }
            if($entrust[$k]['type']==1){
                $entrust[$k]['type']="卖出";

            }
            if($entrust[$k]['state']==0){
                $entrust[$k]['state']="未成交";

            }
            if($entrust[$k]['state']==1){
                $entrust[$k]['state']="已成交";

            }
        }
        $listData['data']=$entrust;

        // 列表批量操作按钮
        //   $this->savePostUrl = U('page/Admin/search');
        $this->displayList($listData);

    }


    function com_cp(){
        $_REQUEST['tabHash'] = 'com_cp';
        // 初始化diy页面列表管理菜单
        $this->_initPageListAdminMenu('com_cp');
        $this->pageButton[] = array('title'=>'搜索平仓情况','onclick'=>"admin.fold('search_form')");
        $this->pageButton[]=array('title'=>'清空所有比赛数据','onclick'=> 'location.href=\''.U('bitcoin/Admin/delete_com_data',array('f'=>'com')).'\'');
        $this->searchKey = array('uname');
        $this->pageKeyList = array('id','uid','uname','type','price','number','c_price','c_time');

        if($_POST['uname'] == null){
            $cp = D('bitcoin_com_closeposition')->order('c_time desc')->select();
            foreach($cp as $k=>$v){
                $condition['uid']=$cp[$k]['uid'];
                $info=D('bitcoin_info')->where($condition)->select();
                $cp[$k]['uname']= $info['0']['uname'];
            }
        }
        else{
        $data3['uname'] = array('like','%'.t($_POST['uname']).'%');
        $user_info=  D('bitcoin_com_info')->where($data3)->select();
        //$data['uname'] = array('like','%'.t($_POST['uname']).'%');
        $data['uid']=$user_info['0']['uid'];
        // 数据的格式化与listKey保持一致
        $cp = D('bitcoin_com_closeposition')->where($data)->order('c_time desc')->select();
            foreach($cp as $k=>$v){
                $cp[$k]['uname']=$user_info['0']['uname'];
            }
        }


        foreach($cp as $k=>$v){
             $cp[ $k]['c_time']= date('Y-m-d H:i',$cp[ $k]['c_time'] );

            if($cp[$k]['type']==0){
                $cp[$k]['type']="做多";

            }
            if($cp[$k]['type']==1){
                $cp[$k]['type']="做空";

            }

        }
        $listData['data']=$cp;

        // 列表批量操作按钮
        //   $this->savePostUrl = U('page/Admin/search');
        $this->displayList($listData);

    }

    function cp(){
        $_REQUEST['tabHash'] = 'cp';
        // 初始化diy页面列表管理菜单
        $this->_initPageListAdminMenu('cp');
        $this->pageButton[] = array('title'=>'搜索平仓情况','onclick'=>"admin.fold('search_form')");
        $this->pageButton[]=array('title'=>'清空模拟数据','onclick'=> 'location.href=\''.U('bitcoin/Admin/delete_com_data',array('f'=>'index')).'\'');
        $this->searchKey = array('uname');
        $this->pageKeyList = array('id','uid','uname','type','price','number','c_price','c_time');


        if($_POST['uname'] == null){
            $cp = D('bitcoin_closeposition')->order('c_time desc')->select();
            foreach($cp as $k=>$v){
                $condition['uid']=$cp[$k]['uid'];
                $info=D('bitcoin_info')->where($condition)->select();
                $cp[$k]['uname']= $info['0']['uname'];
            }
        }
        else{
        $data3['uname'] = array('like','%'.t($_POST['uname']).'%');
        $user_info=  D('bitcoin_info')->where($data3)->select();
        //$data['uname'] = array('like','%'.t($_POST['uname']).'%');
        $data['uid']=$user_info['0']['uid'];
        // 数据的格式化与listKey保持一致
        $cp = D('bitcoin_closeposition')->where($data)->order('c_time desc')->select();
            foreach($cp as $k=>$v){
                $cp[$k]['uname']=$user_info['0']['uname'];
            }
        }

        foreach($cp as $k=>$v){

            $cp[ $k]['c_time']= date('Y-m-d H:i',$cp[ $k]['c_time'] );

            if($cp[$k]['type']==0){
                $cp[$k]['type']="做多";

            }
            if($cp[$k]['type']==1){
                $cp[$k]['type']="做空";

            }

        }
        $listData['data']=$cp;

        // 列表批量操作按钮
        //   $this->savePostUrl = U('page/Admin/search');
        $this->displayList($listData);

    }



    function delete_com_data(){
   if($_GET['f']=='com'){
         $info=D('bitcoin_com_info')->select();
        $delinfo=D('bitcoin_com_info');
        $entrust=D('bitcoin_com_entrust')->select();
       $delentrust= D('bitcoin_com_entrust');
        $cp=D('bitcoin_com_closeposition')->select();
        $delcp= D('bitcoin_com_closeposition');
    }
        if($_GET['f']=='index'){
            $info=D('bitcoin_info')->select();
            $delinfo=D('bitcoin_info');
            $entrust=D('bitcoin_entrust')->select();
            $delentrust= D('bitcoin_entrust');
            $cp=D('bitcoin_closeposition')->select();
            $delcp= D('bitcoin_closeposition');
        }

        foreach($entrust as $k=>$v){
            $condition1['id']=$entrust[$k]['id'];
            $delentrust->where($condition1)->delete();
        }
        foreach($info as $k=>$v){
            $condition2['id']=$info[$k]['id'];
            $delinfo->where($condition2)->delete();
        }
        foreach($cp as $k=>$v){
            $condition3['id']=$cp[$k]['id'];
            $delcp->where($condition3)->delete();
        }
$this->success('清空数据成功');
    }




    function check_ip(){
        $_REQUEST['tabHash'] = 'check_ip';
        // 初始化diy页面列表管理菜单
        $this->_initPageListAdminMenu('check_ip');
        $this->pageButton[] = array('title'=>'批量禁止/取消禁止用户','onclick'=>"admin.ban_com()");
        $this->pageKeyList = array('id','ip','login_time','uid','uname','isban','action');
        $this->_listpk = 'uid';
        $info=D('bitcoin_com_ipcheck')->order('login_ip asc')->select();
        $info1=D('bitcoin_com_ipcheck')->order('login_ip asc')->select();
        foreach($info as $k=>$v){
            foreach($info1 as $key=>$v){
                if(($info[$k]['login_ip'] == $info1[$key]['login_ip']) && ($info[$k]['uid'] != $info1[$key]['uid'])){

                    $listData['data'][$k]['ip']= $info1[$key]['login_ip'];
                    $listData['data'][$k]['uid']=$info[$k]['uid'];
                    $listData['data'][$k]['uname']=$info[$k]['uname'];
                    $listData['data'][$k]['login_time'] =   date('Y-m-d H:i:s',$info[$k]['login_time']);
                    if($info[$k]['isban']==0){
                        $listData['data'][$k]['isban']='允许使用应用';
                    }
                    if($info[$k]['isban']==1){
                        $listData['data'][$k]['isban']='禁止使用应用';
                    }

                    $listData['data'][$k]['action']='<a href="'.U('bitcoin/Admin/banUser1',array('uid'=> $info[$k]['uid'],'f'=>'com')).'">禁止/取消禁止用户</a>';
                }

            }
        }

        $this->displayList($listData);
    }



    public function banUser1(){
        $condition['uid']=$_GET['uid'];
        if($_GET['f']=='index'){
            $info=D('bitcoin_info')->where($condition)->select();
        }
        if($_GET['f']=='com'){
            $info=D('bitcoin_com_info')->where($condition)->select();
        }

        if($info['0']['isban']==0){
            $data['isban']=1;
        }
        else{
            $data['isban']=0;
        }
        if($_GET['f']=='index'){
            $rs=D('bitcoin_info')->where($condition)->save($data);
        }
        if($_GET['f']=='com'){
            $rs=D('bitcoin_com_info')->where($condition)->save($data);
        }
        if($rs) {
            $this->success('操作成功');
      	// 操作失败
        } else {
            $this->error('操作失败');
        }





    }

}
?>